package com.example.xcService.pojo.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.AgentOrder;
import com.example.pojo.enums.OrderEnum;
import com.example.pojo.enums.ProductTypeENUM;
import com.example.service.IAgentOrderService;
import com.example.xcService.pojo.tool.OrderTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = {"order.fanout.queue"} ,containerFactory = "listenerContainerFactory" ,concurrency = "50-100")
@Slf4j
public class OrderFanoutConsumer {

    @Autowired
    OrderTool orderTool;
    @Autowired
    IAgentOrderService agentOrderService;

    @RabbitHandler
    @Async
    public  void SubmitOrder(String orderCode){
       try{
           LambdaQueryWrapper<AgentOrder> query=new LambdaQueryWrapper<AgentOrder>();
           query.eq(AgentOrder::getOrderCode,orderCode);
           query.and(wrappers->wrappers.eq(AgentOrder::getOrderStatus,OrderEnum.WAIT).or().eq(AgentOrder::getOrderStatus,OrderEnum.UNDERWAY).or().eq(AgentOrder::getOrderStatus,OrderEnum.ERROR));
           AgentOrder dbOrder=agentOrderService.getOne(query);
           if (dbOrder!=null){
               if (ProductTypeENUM.Equity.equals(dbOrder.getProductType())){
                   orderTool.Submit(dbOrder);
               }else if (ProductTypeENUM.Card.equals(dbOrder.getProductType())){
                   orderTool.Card(dbOrder);
               }
           }
       }catch (Exception ex){
           log.error("提交订单消息队列出错",ex);
       }
    }
}
